package com.wrial.kind.doublepointer;
/*
 * @Author  Wrial
 * @Date Created in 12:28 2020/8/7
 * @Description  在删除第K个节点上改动而来的
 *
 * 输入： 1->2->3->4->5 和 k = 2
 * 输出： 4
 */

public class Return倒数第K个节点 {

    public int kthToLast(ListNode head, int k) {
        ListNode dump = new ListNode(-1);
        dump.next = head;
        ListNode fast = dump;
        ListNode slow = dump;
        for (int i = 0; i < k; i++) {
            fast = fast.next;
        }

        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow.next.val;

    }


    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }


}
